<?php

namespace App\Console\Commands;

use App\Models\Sect;
use App\Models\SectMember;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class CloseSect extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'batch:closesect';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = '关闭超过24小时筹建门派';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        \DB::transaction(function () {
            $limitTime = Carbon::now()->subHours(24);

            $sectArray = Sect::where([['status', 10], ['created_at', '<', $limitTime]])
                ->pluck('id')
                ->all();
            SectMember::whereIn('sect_id', $sectArray)->delete();
            Sect::destroy($sectArray);
            $this->info('执行成功');

        });

    }
}
